package cn.lena.http;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;

/**
 * @author lena
 * @date 2021/8/11
 */
public class HttpClientTest {

	/**
	 * post请求
	 * @param urlParam
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	public static String sendPost(String urlParam) throws HttpException, IOException {
		// 创建httpClient实例对象
		HttpClient httpClient = new HttpClient();
		// 设置httpClient连接主机服务器超时时间：15000毫秒
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
		// 创建post请求方法实例对象
		PostMethod postMethod = new PostMethod(urlParam);
		// 设置post请求超时时间
		postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
		postMethod.addRequestHeader("Content-Type", "application/json");

		httpClient.executeMethod(postMethod);

		String result = postMethod.getResponseBodyAsString();
		postMethod.releaseConnection();
		return result;
	}

	/**
	 * Get请求
	 * @param url
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 */
	public static String sendGet(String url) throws HttpException, IOException {
		// 创建httpClient实例对象
		HttpClient httpClient = new HttpClient();
		// 设置httpClient连接主机服务器超时时间：15000毫秒
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
		// 创建GET请求方法实例对象
		GetMethod getMethod = new GetMethod(url);
		// 设置post请求超时时间
		getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
		getMethod.addRequestHeader("Content-Type", "application/json");

		httpClient.executeMethod(getMethod);

		String result = getMethod.getResponseBodyAsString();
		getMethod.releaseConnection();
		return result;
	}

	public static void main(String[] args) throws HttpException, IOException {
		String url ="http://39.96.59.198:9000/passage/findPassage/4003";
		//System.out.println(sendPost(url));
		System.out.println(sendGet(url));
	}

}
